package com.tiheima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tiheima.mapper.ClazzMapper;
import com.tiheima.model.dto.clazz.AddClazzDTO;
import com.tiheima.model.dto.clazz.ClazzPageQuery;
import com.tiheima.model.dto.clazz.UpdateClazzDTO;
import com.tiheima.model.entity.Clazz;
import com.tiheima.model.result.PageResult;
import com.tiheima.model.vo.clazzListVO;
import com.tiheima.model.vo.clazzVO;
import com.tiheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ClazzServiceImpl implements ClazzService {
    private final ClazzMapper clazzMapper;

    public ClazzServiceImpl(ClazzMapper clazzMapper) {
        this.clazzMapper = clazzMapper;
    }

    @Override
    public PageResult<clazzListVO> getUsersByPage(ClazzPageQuery clazzPageQuery) {
        int page = clazzPageQuery.getPage();
        int pageSize = clazzPageQuery.getPageSize();
        List<clazzListVO> list = null;
        try {
            // 关键分页设置（必须紧跟在查询方法前）
            PageHelper.startPage(page, pageSize);
            // 执行条件查询
            list = clazzMapper.selectByCondition(clazzPageQuery);
        } catch (Exception e) {
            log.error("分页查询异常", e);
        }

        // 状态
        // @TODO 换成stream流
        List<clazzListVO> vos = null;
        if (list != null) {
            vos = list.stream().peek((dos) -> {
                LocalDate now = LocalDate.now();
                if (now.isBefore(dos.getBeginDate())) {
                    dos.setStatus("未开始");
                } else if (now.isAfter(dos.getEndDate())) {
                    dos.setStatus("已结束");
                } else if (now.isAfter(dos.getBeginDate()) && now.isBefore(dos.getEndDate())) {
                    dos.setStatus("在读");
                }
            }).toList();
        }

        // 获取分页信息
        PageInfo<clazzListVO> pageInfo = new PageInfo<>(vos);

        // 封装自定义分页结果
        return PageResult.of(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public int deleteClazz(Integer id) {
        Clazz clazz = clazzMapper.selectClazzHasEmpById(id);
        if (clazz == null) {
            return clazzMapper.deleteClazz(id);
        }
        return 0;
    }

    @Override
    public void addClazz(AddClazzDTO addClazzDTO) {
        Clazz clazz = new Clazz();
        BeanUtil.copyProperties(addClazzDTO, clazz);
        // 1. 参数校验
        boolean isValid = BeanUtil.hasNullField(clazz, "masterId", "room", "id", "createTime", "updateTime");
        if (isValid) {
            throw new RuntimeException("参数非法");
        }
        // 2. 核心业务处理 ：调用Mapper接口
        // 业务字段补全
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.insertClazz(clazz);
    }

    @Override
    public clazzVO getClazzById(Integer id) {
        Clazz clazz = clazzMapper.selectClazzById(id);
        clazzVO clazzVo = new clazzVO();
        BeanUtil.copyProperties(clazz, clazzVo);
        return clazzVo;
    }

    @Override
    public int updateClazz(UpdateClazzDTO updateClazzDTO) {
        Clazz clazz = new Clazz();
        BeanUtil.copyProperties(updateClazzDTO, clazz);
        boolean isValid = BeanUtil.hasNullField(clazz, "masterId", "room", "createTime", "updateTime");
        if (isValid) {
            throw new RuntimeException("非法参数");
        }
        clazz.setUpdateTime(LocalDateTime.now());
        return clazzMapper.updateClazz(clazz);
    }

    @Override
    public List<Clazz> listClazz() {
        return clazzMapper.selectAllClazz();
    }
}
